﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.App;
using System.Data;
using TimKen.DataAccess;
using System.Data.SqlClient;
using TimKen.DataEntity;
using Common;
namespace TimKen.Web
{
    public partial class Dealers : WebPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string provinceID = Request.QueryString["ProvinceID"];
                if (!string.IsNullOrEmpty(provinceID))
                {
                    BindProvince();
                    this.ddlProvince.SelectedValue = provinceID;
                    BindDealer("");
                    this.rptDealerDataView.DataSource = BindDealerData("View_Dealer", "ProvinceID", "", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, " order by ProvinceID", String.Format(" ProvinceID={0}", provinceID));
                    this.rptDealerDataView.DataBind();
                }
            }
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="columName">列名</param>
        /// <param name="sort">排序</param>
        /// <param name="pageIndex">页数</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="condition">条件</param>
        /// <returns></returns>
        public List<DealerInfo> BindDealerData(string tableName, string columName, string sort, int pageIndex, int pageSize, string condition, string conditionExt)
        {
            this.AspNetPager1.RecordCount = GetTotle();
            SqlParameter[] sqlParameters = new SqlParameter[7];
            sqlParameters[0] = new SqlParameter("@tableName", SqlDbType.NVarChar);
            sqlParameters[0].Value = tableName;
            sqlParameters[1] = new SqlParameter("@columName", SqlDbType.NVarChar);
            sqlParameters[1].Value = columName;
            sqlParameters[2] = new SqlParameter("@sort", SqlDbType.NVarChar);
            sqlParameters[2].Value = sort;
            sqlParameters[3] = new SqlParameter("@pageIndex", SqlDbType.Int);
            sqlParameters[3].Value = pageIndex;
            sqlParameters[4] = new SqlParameter("@pageSize", SqlDbType.Int);
            sqlParameters[4].Value = pageSize;
            sqlParameters[5] = new SqlParameter("@condition", SqlDbType.NVarChar);
            sqlParameters[5].Value = condition;
            sqlParameters[6] = new SqlParameter("@conditionExt", SqlDbType.NVarChar);
            sqlParameters[6].Value = conditionExt;
            SqlDataReader dr = SqlHelper.ExecuteReader(DBSetting.ConText, CommandType.StoredProcedure, "[SP_BindDealerDataTableByPageIndex]", sqlParameters);
            List<DealerInfo> list = new List<DealerInfo>();
            while (dr.Read())
            {
                DealerInfo info = new DealerInfo
                {
                    ProvinceID = dr["ProvinceID"].ToString(),
                    DealerLive = dr["DealerLive"].ToString(),
                    ProvinceName = dr["ProvinceName"].ToString(),
                    PhoneNo = dr["PhoneNo"].ToString(),
                    CompanyName = dr["CompanyName"].ToString(),
                    dealerName = dr["dealerName"].ToString()
                };
                list.Add(info);
            }
            return list;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            this.rptDealerDataView.DataSource = BindDealerData("View_Dealer", "ProvinceID", "", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, " order by ProvinceID", String.Format(" ProvinceID={0}", this.ddlProvince.SelectedValue));
            this.rptDealerDataView.DataBind();
        }

        private void BindProvince()
        {
            DataSet pds = DbHelperSQL.Query("SELECT DISTINCT( ProvinceID),ProvinceName from View_Dealer order by ProvinceName");
            this.ddlProvince.Items.Clear();
            this.ddlProvince.DataValueField = "ProvinceID";
            this.ddlProvince.DataTextField = "ProvinceName";
            this.ddlProvince.DataSource = pds;
            this.ddlProvince.DataBind();
            //this.ddlProvince.Items.Insert(0, new ListItem("授权区域"));
        }

        protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
        {
            string provinceID = this.ddlProvince.SelectedValue;
            string dealerName = this.ddlDealerName.SelectedItem.Text;
            this.rptDealerDataView.DataSource = BindDealerData("View_Dealer", "ProvinceID", "", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, " order by ProvinceID", String.Format(" ProvinceID={0} AND DealerName='{1}'", provinceID, dealerName));
            this.rptDealerDataView.DataBind();
        }

        //protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    BindDealer(this.ddlProvince.SelectedValue);
        //}


        public int GetTotle()
        {
            string cmdText = "select COUNT(*) from View_Dealer where 1=1";
            if (!string.IsNullOrEmpty(this.ddlProvince.SelectedValue))
            {
                cmdText += " and ProvinceID = '" + this.ddlProvince.SelectedValue + "'";
            }
            if (!string.IsNullOrEmpty(this.ddlDealerName.SelectedItem.Text))
            {
                cmdText += " and DealerName = '" + this.ddlDealerName.SelectedItem.Text + "'";
            }
            return Convert.ToInt32(SqlHelper.ExecuteScalar(DBSetting.ConText, CommandType.Text, cmdText));
        }

        private List<DealerInfo> BindDealerData(string provinceID)
        {
            string sql = "";
            if (string.IsNullOrEmpty(provinceID))
            {
                sql = String.Format("select DISTINCT(dealerName) from View_Dealer");
            }
            else
            {
                sql = String.Format("select DISTINCT(dealerName) from View_Dealer  where provinceID ={0}", provinceID);
            }
            SqlDataReader dr = DbHelperSQL.ExecuteReader(sql);
            List<DealerInfo> list = new List<DealerInfo>();
            while (dr.Read())
            {
                DealerInfo info = new DealerInfo
                {
                    // ProvinceID = dr["ProvinceID"].ToString(),
                    dealerName = dr["dealerName"].ToString()
                };
                list.Add(info);
            }
            return list;
        }
        private void BindDealer(string num)
        {
            this.ddlDealerName.DataValueField = "dealerName";
            this.ddlDealerName.DataTextField = "dealerName";
            this.ddlDealerName.DataSource = BindDealerData(num);
            this.ddlDealerName.DataBind();
        }
    }
}